{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8465dc9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c:\\\\repos\\\\scripts\\\\wp\\\\woocommerce\\\\woo_df\\\\archives\\\\ipynbs'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "df5149bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "id",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "image",
         "rawType": "object",
         "type": "string"
        }
       ],
       "ref": "e9a00555-360f-40ca-b0f6-1ff67afe31a1",
       "rows": [
        [
         "0",
         "1",
         "abc.jpg"
        ],
        [
         "1",
         "2",
         "df.gif.png"
        ],
        [
         "2",
         "3",
         "x.unknowfmt"
        ],
        [
         "3",
         "4",
         "noextension"
        ],
        [
         "4",
         "5",
         "endonlydot"
        ],
        [
         "5",
         "6",
         "x.fieldname.keepit"
        ]
       ],
       "shape": {
        "columns": 2,
        "rows": 6
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>image</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>abc.jpg</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>df.gif.png</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>x.unknowfmt</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>noextension</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>endonlydot</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>x.fieldname.keepit</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id               image\n",
       "0   1             abc.jpg\n",
       "1   2          df.gif.png\n",
       "2   3         x.unknowfmt\n",
       "3   4         noextension\n",
       "4   5          endonlydot\n",
       "5   6  x.fieldname.keepit"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "df=pd.read_csv(\"../../csv_dir/test_extension.csv\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a375424f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "image",
         "rawType": "object",
         "type": "unknown"
        }
       ],
       "ref": "3cb08490-1f30-4a71-99a9-3e2b5a3d5910",
       "rows": [
        [
         "0",
         "['abc', 'jpg']"
        ],
        [
         "1",
         "['df.gif', 'png']"
        ],
        [
         "2",
         "['x', 'unknowfmt']"
        ],
        [
         "3",
         "['noextension']"
        ],
        [
         "4",
         "['endonlydot']"
        ],
        [
         "5",
         "['x.fieldname', 'keepit']"
        ]
       ],
       "shape": {
        "columns": 1,
        "rows": 6
       }
      },
      "text/plain": [
       "0               [abc, jpg]\n",
       "1            [df.gif, png]\n",
       "2           [x, unknowfmt]\n",
       "3            [noextension]\n",
       "4             [endonlydot]\n",
       "5    [x.fieldname, keepit]\n",
       "Name: image, dtype: object"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['image'].str.rsplit('.', n=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b24db93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "id",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "image",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "formated_image",
         "rawType": "object",
         "type": "string"
        }
       ],
       "ref": "cff70f47-521a-460f-bde3-e606dc9a38f8",
       "rows": [
        [
         "0",
         "1",
         "abc.jpg",
         "abc.webp"
        ],
        [
         "1",
         "2",
         "df.gif.png",
         "df.gif.webp"
        ],
        [
         "2",
         "3",
         "x.unknowfmt",
         "x.unknowfmt.webp"
        ],
        [
         "3",
         "4",
         "noextension",
         "noextension.webp"
        ],
        [
         "4",
         "5",
         "endonlydot",
         "endonlydot.webp"
        ],
        [
         "5",
         "6",
         "x.fieldname.keepit",
         "x.fieldname.keepit.webp"
        ]
       ],
       "shape": {
        "columns": 3,
        "rows": 6
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>image</th>\n",
       "      <th>formated_image</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>abc.jpg</td>\n",
       "      <td>abc.webp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>df.gif.png</td>\n",
       "      <td>df.gif.webp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>x.unknowfmt</td>\n",
       "      <td>x.unknowfmt.webp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>noextension</td>\n",
       "      <td>noextension.webp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>endonlydot</td>\n",
       "      <td>endonlydot.webp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>x.fieldname.keepit</td>\n",
       "      <td>x.fieldname.keepit.webp</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id               image           formated_image\n",
       "0   1             abc.jpg                 abc.webp\n",
       "1   2          df.gif.png              df.gif.webp\n",
       "2   3         x.unknowfmt         x.unknowfmt.webp\n",
       "3   4         noextension         noextension.webp\n",
       "4   5          endonlydot          endonlydot.webp\n",
       "5   6  x.fieldname.keepit  x.fieldname.keepit.webp"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果image字段列中的文件名字符串以常见的图片格式结尾,则丢弃后缀仅保留文件名,否则认为这个字符串不带格式扩展名,保留原样\n",
    "supported_image_formats = [\"jpg\", \"jpeg\", \"png\", \"tif\", \"tiff\", \"gif\"]\n",
    "default_image_format = \"webp\"\n",
    "df[\"formated_image\"] = (\n",
    "    df[\"image\"].apply(\n",
    "        lambda x: (\n",
    "            x.rsplit(\".\", 1)[-2] if x.split(\".\")[-1] in supported_image_formats else x\n",
    "        )\n",
    "    )\n",
    "    + f\".{default_image_format}\"\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "bd8412d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['abc.jpg',\n",
       " 'df.gif.png',\n",
       " 'x.unknowfmt',\n",
       " 'noextension',\n",
       " 'endonlydot',\n",
       " 'x.fieldname.keepit']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(df[\"image\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "b0a53f58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "0",
         "rawType": "object",
         "type": "string"
        }
       ],
       "ref": "6f6d17d9-e4b3-41d4-af67-b69edc2a9e00",
       "rows": [
        [
         "0",
         "abc.jpg"
        ],
        [
         "1",
         "df.gif.jpg"
        ],
        [
         "2",
         "x.unknowfmt.jpg"
        ],
        [
         "3",
         "noextension.jpg"
        ],
        [
         "4",
         "endonlydot.jpg"
        ],
        [
         "5",
         "x.fieldname.keepit.jpg"
        ]
       ],
       "shape": {
        "columns": 1,
        "rows": 6
       }
      },
      "text/plain": [
       "0                   abc.jpg\n",
       "1                df.gif.jpg\n",
       "2           x.unknowfmt.jpg\n",
       "3           noextension.jpg\n",
       "4            endonlydot.jpg\n",
       "5    x.fieldname.keepit.jpg\n",
       "dtype: object"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "supported_image_formats = [\"jpg\", \"jpeg\", \"png\", \"tif\", \"tiff\", \"gif\"]\n",
    "from pandas import Series\n",
    "\n",
    "\n",
    "def set_image_extension(series: Series, default_image_format, supported_image_formats):\n",
    "    \"\"\"\n",
    "    批量设置图片字段列的图片格式\n",
    "    如果image字段列中的文件名字符串以常见的图片格式结尾,则丢弃后缀仅保留文件名,否则认为这个字符串不带格式扩展名,保留原样\"\"\"\n",
    "\n",
    "    res = (\n",
    "        series.apply(\n",
    "            lambda x: (\n",
    "                x.rsplit(\".\", 1)[-2]\n",
    "                if x.split(\".\")[-1] in supported_image_formats\n",
    "                else x\n",
    "            )\n",
    "        )\n",
    "        + f\".{default_image_format}\"\n",
    "    )\n",
    "\n",
    "    return res\n",
    "\n",
    "\n",
    "test_series = [\n",
    "    \"abc.jpg\",\n",
    "    \"df.gif.png\",\n",
    "    \"x.unknowfmt\",\n",
    "    \"noextension\",\n",
    "    \"endonlydot\",\n",
    "    \"x.fieldname.keepit\",\n",
    "]\n",
    "set_image_extension(\n",
    "    Series(test_series), \"jpg\", supported_image_formats\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f449bfbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "image",
         "rawType": "object",
         "type": "string"
        }
       ],
       "ref": "2d262fe5-9c2b-4098-9f11-131165d09d33",
       "rows": [
        [
         "0",
         "abc.webp"
        ],
        [
         "1",
         "df.gif.webp"
        ],
        [
         "2",
         "x.webp"
        ],
        [
         "3",
         "noextension.webp"
        ],
        [
         "4",
         "endonlydot.webp"
        ]
       ],
       "shape": {
        "columns": 1,
        "rows": 5
       }
      },
      "text/plain": [
       "0            abc.webp\n",
       "1         df.gif.webp\n",
       "2              x.webp\n",
       "3    noextension.webp\n",
       "4     endonlydot.webp\n",
       "Name: image, dtype: object"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ...existing code...\n",
    "df['image'].str.rsplit('.', n=1).str[0] + '.webp'\n",
    "# ...existing code..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "1bb51da1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['xyz/wp.abc', 'jpg']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"xyz/wp.abc.jpg\".rsplit('.', 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2eecf0d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['abc']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"abc\".rsplit('.', 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b41b7d7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
